package com.judian.smart.util;

import android.bluetooth.BluetoothAdapter;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.AudioManager;

import com.judian.smart.base.MyApplication;

/**
 * author: Peter
 * date: 2019/08/14 星期三 16:59
 * desc:
 */
public class AudioManagerUtil {
    private static final String TAG = AudioManagerUtil.class.getSimpleName();
    private boolean bluetoothIsConnected = false;
    private BluetoothConnectionReceiver audioNoisyReceiver;
    private AudioManager audioManager = null;
    private static AudioManagerUtil ins = new AudioManagerUtil();

    public static AudioManagerUtil get() {
        return ins;
    }

    /**
     * 设置蓝牙连接状态广播
     */
    public void registerReceiver() {
        audioNoisyReceiver = new BluetoothConnectionReceiver();   //蓝牙状态广播监听
        IntentFilter audioFilter = new IntentFilter();
        audioFilter.addAction(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED);
        audioFilter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
        MyApplication.getInstance().registerReceiver(audioNoisyReceiver, audioFilter);
    }

    /**
     * 取消注册蓝牙连接广播
     */
    public void unRegisterReceiver() {
        MyApplication.getInstance().unregisterReceiver(audioNoisyReceiver);
    }

    /**
     * 获取audioManager对象,以便后续操作
     */
    public AudioManagerUtil getAudioManager() {
        if (audioManager == null) {
            audioManager = (AudioManager) MyApplication.getInstance().getSystemService(Context.AUDIO_SERVICE);
        }
        return this;
    }

    /**
     * 输出源切换到外放
     */
    public void changeToSpeaker() {
        //注意此处，蓝牙未断开时使用MODE_IN_COMMUNICATION而不是MODE_NORMAL
        audioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
        audioManager.stopBluetoothSco();
        audioManager.setBluetoothScoOn(false);
        audioManager.setSpeakerphoneOn(true);
    }

    /**
     * 输出源切换到蓝牙设备
     */
    public void changeToHeadSet() {
        audioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
        audioManager.startBluetoothSco();
        audioManager.setBluetoothScoOn(true);
        audioManager.setSpeakerphoneOn(false);

    }

    class BluetoothConnectionReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            if (BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED.equals(intent.getAction())) {    //蓝牙连接状态
                int state = intent.getIntExtra(BluetoothAdapter.EXTRA_CONNECTION_STATE, -1);
                if (state == BluetoothAdapter.STATE_CONNECTED) {
                    //已连接，切换音频输出
                    bluetoothIsConnected = true;
                } else if (state == BluetoothAdapter.STATE_DISCONNECTED) {
                    //已断开
                    bluetoothIsConnected = false;
                }

            } else if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(intent.getAction())) {  //本地蓝牙打开或关闭
                int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1);
                if (state == BluetoothAdapter.STATE_OFF || state == BluetoothAdapter.STATE_TURNING_OFF) {
                    //蓝牙关闭/正在关闭，切换音频输出
                    bluetoothIsConnected = false;
                }
            }
        }
    }
}
